package com.ruoyi.project.system.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysAttr;
import com.ruoyi.project.system.domain.SysPost;
import com.ruoyi.project.system.service.ISysAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 属性操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/attr")
public class SysAttrController  extends BaseController {
    @Autowired
    private ISysAttrService attrService;
    /**
     * 获取属性列表
     */
    @PreAuthorize("@ss.hasPermi('system:attr:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAttr attr)
    {
        startPage();
        List<SysAttr> list = attrService.selectAttrList(attr);
        return getDataTable(list);
    }
    /**
     * 根据属性
     */
    @PreAuthorize("@ss.hasPermi('system:attr:query')")
    @GetMapping(value = "/{attrId}")
    public AjaxResult getInfo(@PathVariable Long attrId)
    {
        return AjaxResult.success(attrService.selectAttrById(attrId));
    }
    /**
     * 新增属性
     */
    @PreAuthorize("@ss.hasPermi('system:attr:add')")
    @Log(title = "属性管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAttr attr)
    {
        if (UserConstants.NOT_UNIQUE.equals(attrService.checkAttrNameUnique(attr)))
        {
            return AjaxResult.error("新增管理'" + attr.getName() + "'失败，属性名称已存在");
        }
        attr.setCreateBy(SecurityUtils.getUsername());
        return toAjax(attrService.insertAttr(attr));
    }
    /**
     * 修改属性
     */
    @PreAuthorize("@ss.hasPermi('system:attr:edit')")
    @Log(title = "属性管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAttr attr)
    {
        if (UserConstants.NOT_UNIQUE.equals(attrService.checkAttrNameUnique(attr)))
        {
            return AjaxResult.error("修改属性'" + attr.getIconName() + "'失败，属性名称已存在");
        }
        attr.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(attrService.updateAttr(attr));
    }
    /**
     * 删除属性
     */
    @PreAuthorize("@ss.hasPermi('system:attr:remove')")
    @Log(title = "属性管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attrId}")
    public AjaxResult remove(@PathVariable Long attrId)
    {
        return toAjax(attrService.deleteAttrById(attrId));
    }
}
